﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LRSystem
{
    public static class PacketClassDictionary
    {
        private static Dictionary<byte, Type> pkdic = new Dictionary<byte, Type>();
        static PacketClassDictionary()
        {
            AddPacketClass(EnrollBeacon._type, typeof(EnrollBeacon));
            AddPacketClass(EnrollRequest._type, typeof(EnrollRequest));
            AddPacketClass(EnrollResponse._type, typeof(EnrollResponse));
            AddPacketClass(DataBeacon._type, typeof(DataBeacon));
            AddPacketClass(ConfirmedDataUp._type, typeof(ConfirmedDataUp));
        }

        public static void AddPacketClass(byte tp, Type classtp)
        {
            if (pkdic.ContainsKey(tp) == false)
                pkdic.Add(tp, classtp);
        }

        public static object CreateInstanse(byte tp)   // 根据类型创建类实例
        {
            if (pkdic.ContainsKey(tp))
                return Activator.CreateInstance(pkdic[tp]);
            else
                return null;
        }

        public static void RemovePacketClass(byte tp)  // 根据类型从字典中删除相应类
        {
            pkdic.Remove(tp);
        }
    }
}
